<template>
    <div class="dragdesign-webPoster-container">
        <el-dialog title="新增WEB海报" v-model="isShowDialog" >
            <el-form ref="addWebPosterFormRef" :model="formData" :rules="formRules" size="small" label-width="80px">
                <el-row :gutter="35">
                    <el-col class="mb20">
                        <el-form-item label="标题" prop="title">
                            <el-input v-model="formData.title" placeholder="请输入标题" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col class="mb20">
                        <el-form-item label="备注" prop="remark">
                            <el-input type="textarea" v-model="formData.remark" placeholder="请输入备注" clearable></el-input>
                        </el-form-item>
                    </el-col>
					<el-col class="mb20">
						<div style="width: 300px;margin: 0 auto;">
							<tool-single-img v-model:imageUrl='formData.cover' tip="封面建议尺寸: 16 : 9等比例图片, 高度自适应"></tool-single-img>
						</div>
					</el-col>
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="onCancel" size="small">取 消</el-button>
                    <el-button type="primary" @click="onSubmit" size="small">保 存</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts">
import { reactive, toRefs, ref } from 'vue';
import { ElMessage } from 'element-plus';
import { addWebPoster } from '/@/api/dragdesign/poster/index';
import ToolSingleImg from '/@/views/decorator/components/toolBar/module/tool-single-img.vue';
export default {
    name: 'dragdesign/poster/add',
	components: { ToolSingleImg },
    setup(props, context) {
        const addWebPosterFormRef = ref();
        const state = reactive({
            isShowDialog: false,
            formData: {
            },
            formRules : {
            }
        });

        
        // 打开弹窗
        const openDialog = (row?: object) => {
            state.isShowDialog = true;
        };
        // 关闭弹窗
        const closeDialog = (row?: object) => {
            state.isShowDialog = false;
            for(var key in state.formData) {
                state.formData[key] = undefined;
            }
            addWebPosterFormRef.value.resetFields();
        };
        // 取消
        const onCancel = () => {
            closeDialog();
        };
        // 新增
        const onSubmit = () => {
            addWebPosterFormRef.value.validate(valid => {
                if(valid){
                    let params = {
                        title: state.formData.title,
                        remark: state.formData.remark,
						cover: state.formData.cover,
                    }
                    addWebPoster(params).then((res) => {
                        if(res.code === 0){
                            ElMessage.success('保存成功');
                            context.emit('submit');
                            closeDialog();
                        }
                    });
                }
            });
        };
        return {
            addWebPosterFormRef,
            openDialog,
            closeDialog,
            onCancel,
            onSubmit,
            ...toRefs(state),
        };
    },
};
</script>